home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-1.iso / compress / gnucpio.zip / UTIL.C < prev    next >
C/C++ Source or Header  |  1996-01-01  |  38KB  |  1,341 lines

  1. /* util.c - Several utility routines for cpio.
  2.    Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <stdio.h>
  19. #include <sys/types.h>
  20. #ifdef HPUX_CDF
  21. #include <sys/stat.h>
  22. #endif
  23. #include "system.h"
  24. #include "cpiohdr.h"
  25. #include "dstring.h"
  26. #include "extern.h"
  27. #include "rmt.h"
  28.  
  29. #ifndef __MSDOS__
  30. #include <sys/ioctl.h>
  31. #else
  32. #include <io.h>
  33. #endif
  34.  
  35. #ifdef HAVE_SYS_MTIO_H
  36. #ifdef HAVE_SYS_IO_TRIOCTL_H
  37. #include <sys/io/trioctl.h>
  38. #endif
  39. #include <sys/mtio.h>
  40. #endif
  41.  
  42. static void tape_fill_input_buffer P_((int in_des, int num_bytes));
  43. static int disk_fill_input_buffer P_((int in_des, int num_bytes));
  44. static void hash_insert ();
  45. static void write_nuls_to_file P_((long num_bytes, int out_des));
  46.  
  47. /* Write `output_size' bytes of `output_buffer' to file
  48.    descriptor OUT_DES and reset `output_size' and `out_buff'.  */
  49.  
  50. void
  51. tape_empty_output_buffer (out_des)
  52.      int out_des;
  53. {
  54.   int bytes_written;
  55.  
  56. #ifdef BROKEN_LONG_TAPE_DRIVER
  57.   static long output_bytes_before_lseek = 0;
  58.  
  59.   /* Some tape drivers seem to have a signed internal seek pointer and
  60.      they lose if it overflows and becomes negative (e.g. when writing 
  61.      tapes > 2Gb).  Doing an lseek (des, 0, SEEK_SET) seems to reset the 
  62.      seek pointer and prevent it from overflowing.  */
  63.   if (output_is_special
  64.      && ( (output_bytes_before_lseek += output_size) >= 1073741824L) )
  65.     {
  66.       lseek(out_des, 0L, SEEK_SET);
  67.       output_bytes_before_lseek = 0;
  68.     }
  69. #endif
  70.  
  71.   bytes_written = rmtwrite (out_des, output_buffer, output_size);
  72.   if (bytes_written != output_size)
  73.     {
  74.       int rest_bytes_written;
  75.       int rest_output_size;
  76.  
  77.       if (output_is_special
  78.       && (bytes_written >= 0
  79.           || (bytes_written < 0
  80.           && (errno == ENOSPC || errno == EIO || errno == ENXIO))))
  81.     {
  82.       get_next_reel (out_des);
  83.       if (bytes_written > 0)
  84.         rest_output_size = output_size - bytes_written;
  85.       else
  86.         rest_output_size = output_size;
  87.       rest_bytes_written = rmtwrite (out_des, output_buffer,
  88.                      rest_output_size);
  89.       if (rest_bytes_written != rest_output_size)
  90.         error (1, errno, "write error");
  91.     }
  92.       else
  93.     error (1, errno, "write error");
  94.     }
  95.   output_bytes += output_size;
  96.   out_buff = output_buffer;
  97.   output_size = 0;
  98. }
  99.  
  100. /* Write `output_size' bytes of `output_buffer' to file
  101.    descriptor OUT_DES and reset `output_size' and `out_buff'.
  102.    If `swapping_halfwords' or `swapping_bytes' is set,
  103.    do the appropriate swapping first.  Our callers have
  104.    to make sure to only set these flags if `output_size' 
  105.    is appropriate (a multiple of 4 for `swapping_halfwords',
  106.    2 for `swapping_bytes').  The fact that DISK_IO_BLOCK_SIZE
  107.    must always be a multiple of 4 helps us (and our callers)
  108.    insure this.  */
  109.  
  110. void
  111. disk_empty_output_buffer (out_des)
  112.      int out_des;
  113. {
  114.   int bytes_written;
  115.  
  116.   if (swapping_halfwords || swapping_bytes)
  117.     {
  118.       if (swapping_halfwords)
  119.     {
  120.       int complete_words;
  121.       complete_words = output_size / 4;
  122.       swahw_array (output_buffer, complete_words);
  123.       if (swapping_bytes)
  124.         swab_array (output_buffer, 2 * complete_words);
  125.     }
  126.       else
  127.     {
  128.       int complete_halfwords;
  129.       complete_halfwords = output_size /2;
  130.       swab_array (output_buffer, complete_halfwords);
  131.     }
  132.     }
  133.  
  134.   if (sparse_flag)
  135.     bytes_written = sparse_write (out_des, output_buffer, output_size);
  136.   else
  137.     bytes_written = write (out_des, output_buffer, output_size);
  138.  
  139.   if (bytes_written != output_size)
  140.     {
  141.       error (1, errno, "write error");
  142.     }
  143.   output_bytes += output_size;
  144.   out_buff = output_buffer;
  145.   output_size = 0;
  146. }
  147.  
  148. /* Exchange the halfwords of each element of the array of COUNT longs
  149.    starting at PTR.  PTR does not have to be aligned at a word
  150.    boundary.  */
  151.  
  152. void
  153. swahw_array (ptr, count)
  154.      char *ptr;
  155.      int count;
  156. {
  157.   char tmp;
  158.  
  159.   for (; count > 0; --count)
  160.     {
  161.       tmp = *ptr;
  162.       *ptr = *(ptr + 2);
  163.       *(ptr + 2) = tmp;
  164.       ++ptr;
  165.       tmp = *ptr;
  166.       *ptr = *(ptr + 2);
  167.       *(ptr + 2) = tmp;
  168.       ptr += 3;
  169.     }
  170. }
  171.  
  172. /* Read at most NUM_BYTES or `io_block_size' bytes, whichever is smaller,
  173.    into the start of `input_buffer' from file descriptor IN_DES.
  174.    Set `input_size' to the number of bytes read and reset `in_buff'.
  175.    Exit with an error if end of file is reached.  */
  176.  
  177. #ifdef BROKEN_LONG_TAPE_DRIVER
  178. static long input_bytes_before_lseek = 0;
  179. #endif
  180.  
  181. static void
  182. tape_fill_input_buffer (in_des, num_bytes)
  183.      int in_des;
  184.      int num_bytes;
  185. {
  186. #ifdef BROKEN_LONG_TAPE_DRIVER
  187.   /* Some tape drivers seem to have a signed internal seek pointer and
  188.      they lose if it overflows and becomes negative (e.g. when writing 
  189.      tapes > 4Gb).  Doing an lseek (des, 0, SEEK_SET) seems to reset the 
  190.      seek pointer and prevent it from overflowing.  */
  191.   if (input_is_special
  192.       && ( (input_bytes_before_lseek += num_bytes) >= 1073741824L) )
  193.     {
  194.       lseek(in_des, 0L, SEEK_SET);
  195.       input_bytes_before_lseek = 0;
  196.     }
  197. #endif
  198.   in_buff = input_buffer;
  199.   num_bytes = (num_bytes < io_block_size) ? num_bytes : io_block_size;
  200.   input_size = rmtread (in_des, input_buffer, num_bytes);
  201.   if (input_size == 0 && input_is_special)
  202.     {
  203.       get_next_reel (in_des);
  204.       input_size = rmtread (in_des, input_buffer, num_bytes);
  205.     }
  206.   if (input_size < 0)
  207.     error (1, errno, "read error");
  208.   if (input_size == 0)
  209.     {
  210.       error (0, 0, "premature end of file");
  211.       exit (1);
  212.     }
  213.   input_bytes += input_size;
  214. }
  215.  
  216. /* Read at most NUM_BYTES or `DISK_IO_BLOCK_SIZE' bytes, whichever is smaller,
  217.    into the start of `input_buffer' from file descriptor IN_DES.
  218.    Set `input_size' to the number of bytes read and reset `in_buff'.
  219.    Exit with an error if end of file is reached.  */
  220.  
  221. static int
  222. disk_fill_input_buffer (in_des, num_bytes)
  223.      int in_des;
  224.      int num_bytes;
  225. {
  226.   in_buff = input_buffer;
  227.   num_bytes = (num_bytes < DISK_IO_BLOCK_SIZE) ? num_bytes : DISK_IO_BLOCK_SIZE;
  228.   input_size = read (in_des, input_buffer, num_bytes);
  229.   if (input_size < 0)
  230.     return (-1);
  231.   else if (input_size == 0)
  232.     return (1);
  233.   input_bytes += input_size;
  234.   return (0);
  235. }
  236.  
  237. /* Copy NUM_BYTES of buffer IN_BUF to `out_buff', which may be partly full.
  238.    When `out_buff' fills up, flush it to file descriptor OUT_DES.  */
  239.  
  240. void
  241. tape_buffered_write (in_buf, out_des, num_bytes)
  242.      char *in_buf;
  243.      int out_des;
  244.      long num_bytes;
  245. {
  246.   register long bytes_left = num_bytes;    /* Bytes needing to be copied.  */
  247.   register long space_left;    /* Room left in output buffer.  */
  248.  
  249.   while (bytes_left > 0)
  250.     {
  251.       space_left = io_block_size - output_size;
  252.       if (space_left == 0)
  253.     tape_empty_output_buffer (out_des);
  254.       else
  255.     {
  256.       if (bytes_left < space_left)
  257.         space_left = bytes_left;
  258.       bcopy (in_buf, out_buff, (unsigned) space_left);
  259.       out_buff += space_left;
  260.       output_size += space_left;
  261.       in_buf += space_left;
  262.       bytes_left -= space_left;
  263.     }
  264.     }
  265. }
  266.  
  267. /* Copy NUM_BYTES of buffer IN_BUF to `out_buff', which may be partly full.
  268.    When `out_buff' fills up, flush it to file descriptor OUT_DES.  */
  269.  
  270. void
  271. disk_buffered_write (in_buf, out_des, num_bytes)
  272.      char *in_buf;
  273.      int out_des;
  274.      long num_bytes;
  275. {
  276.   register long bytes_left = num_bytes;    /* Bytes needing to be copied.  */
  277.   register long space_left;    /* Room left in output buffer.  */
  278.  
  279.   while (bytes_left > 0)
  280.     {
  281.       space_left = DISK_IO_BLOCK_SIZE - output_size;
  282.       if (space_left == 0)
  283.     disk_empty_output_buffer (out_des);
  284.       else
  285.     {
  286.       if (bytes_left < space_left)
  287.         space_left = bytes_left;
  288.       bcopy (in_buf, out_buff, (unsigned) space_left);
  289.       out_buff += space_left;
  290.       output_size += space_left;
  291.       in_buf += space_left;
  292.       bytes_left -= space_left;
  293.     }
  294.     }
  295. }
  296.  
  297. /* Copy NUM_BYTES of buffer `in_buff' into IN_BUF.
  298.    `in_buff' may be partly full.
  299.    When `in_buff' is exhausted, refill it from file descriptor IN_DES.  */
  300.  
  301. void
  302. tape_buffered_read (in_buf, in_des, num_bytes)
  303.      char *in_buf;
  304.      int in_des;
  305.      long num_bytes;
  306. {
  307.   register long bytes_left = num_bytes;    /* Bytes needing to be copied.  */
  308.   register long space_left;    /* Bytes to copy from input buffer.  */
  309.  
  310.   while (bytes_left > 0)
  311.     {
  312.       if (input_size == 0)
  313.     tape_fill_input_buffer (in_des, io_block_size);
  314.       if (bytes_left < input_size)
  315.     space_left = bytes_left;
  316.       else
  317.     space_left = input_size;
  318.       bcopy (in_buff, in_buf, (unsigned) space_left);
  319.       in_buff += space_left;
  320.       in_buf += space_left;
  321.       input_size -= space_left;
  322.       bytes_left -= space_left;
  323.     }
  324. }
  325.  
  326. /* Copy the the next NUM_BYTES bytes of `input_buffer' into PEEK_BUF.
  327.    If NUM_BYTES bytes are not available, read the next `io_block_size' bytes
  328.    into the end of `input_buffer' and update `input_size'.
  329.  
  330.    Return the number of bytes copied into PEEK_BUF.
  331.    If the number of bytes returned is less than NUM_BYTES,
  332.    then EOF has been reached.  */
  333.  
  334. int
  335. tape_buffered_peek (peek_buf, in_des, num_bytes)
  336.      char *peek_buf;
  337.      int in_des;
  338.      int num_bytes;
  339. {
  340.   long tmp_input_size;
  341.   long got_bytes;
  342.   char *append_buf;
  343.  
  344. #ifdef BROKEN_LONG_TAPE_DRIVER
  345.   /* Some tape drivers seem to have a signed internal seek pointer and
  346.      they lose if it overflows and becomes negative (e.g. when writing 
  347.      tapes > 4Gb).  Doing an lseek (des, 0, SEEK_SET) seems to reset the 
  348.      seek pointer and prevent it from overflowing.  */
  349.   if (input_is_special
  350.       && ( (input_bytes_before_lseek += num_bytes) >= 1073741824L) )
  351.     {
  352.       lseek(in_des, 0L, SEEK_SET);
  353.       input_bytes_before_lseek = 0;
  354.     }
  355. #endif
  356.  
  357.   while (input_size < num_bytes)
  358.     {
  359.       append_buf = in_buff + input_size;
  360.       if ( (append_buf - input_buffer) >= input_buffer_size)
  361.     {
  362.       /* We can keep up to 2 "blocks" (either the physical block size
  363.          or 512 bytes(the size of a tar record), which ever is
  364.          larger) in the input buffer when we are peeking.  We
  365.          assume that our caller will never be interested in peeking
  366.          ahead at more than 512 bytes, so we know that by the time
  367.          we need a 3rd "block" in the buffer we can throw away the
  368.          first block to make room.  */
  369.       int half;
  370.       half = input_buffer_size / 2;
  371.       bcopy (input_buffer + half, input_buffer, half);
  372.       in_buff = in_buff - half;
  373.       append_buf = append_buf - half;
  374.     }
  375.       tmp_input_size = rmtread (in_des, append_buf, io_block_size);
  376.       if (tmp_input_size == 0)
  377.     {
  378.       if (input_is_special)
  379.         {
  380.           get_next_reel (in_des);
  381.           tmp_input_size = rmtread (in_des, append_buf, io_block_size);
  382.         }
  383.       else
  384.         break;
  385.     }
  386.       if (tmp_input_size < 0)
  387.     error (1, errno, "read error");
  388.       input_bytes += tmp_input_size;
  389.       input_size += tmp_input_size;
  390.     }
  391.   if (num_bytes <= input_size)
  392.     got_bytes = num_bytes;
  393.   else
  394.     got_bytes = input_size;
  395.   bcopy (in_buff, peek_buf, (unsigned) got_bytes);
  396.   return got_bytes;
  397. }
  398.  
  399. /* Skip the next NUM_BYTES bytes of file descriptor IN_DES.  */
  400.  
  401. void
  402. tape_toss_input (in_des, num_bytes)
  403.      int in_des;
  404.      long num_bytes;
  405. {
  406.   register long bytes_left = num_bytes;    /* Bytes needing to be copied.  */
  407.   register long space_left;    /* Bytes to copy from input buffer.  */
  408.  
  409.   while (bytes_left > 0)
  410.     {
  411.       if (input_size == 0)
  412.     tape_fill_input_buffer (in_des, io_block_size);
  413.       if (bytes_left < input_size)
  414.     space_left = bytes_left;
  415.       else
  416.     space_left = input_size;
  417.  
  418.       if (crc_i_flag && only_verify_crc_flag)
  419.     {
  420.        int k;
  421.       for (k = 0; k < space_left; ++k)
  422.         crc += in_buff[k] & 0xff;
  423.     }
  424.  
  425.       in_buff += space_left;
  426.       input_size -= space_left;
  427.       bytes_left -= space_left;
  428.     }
  429. }
  430.  
  431. /* Copy a file using the input and output buffers, which may start out
  432.    partly full.  After the copy, the files are not closed nor the last
  433.    block flushed to output, and the input buffer may still be partly
  434.    full.  If `crc_i_flag' is set, add each byte to `crc'.
  435.    IN_DES is the file descriptor for input;
  436.    OUT_DES is the file descriptor for output;
  437.    NUM_BYTES is the number of bytes to copy.  */
  438.  
  439. void
  440. copy_files_tape_to_disk (in_des, out_des, num_bytes)
  441.      int in_des;
  442.      int out_des;
  443.      long num_bytes;
  444. {
  445.   long size;
  446.   long k;
  447.  
  448.   while (num_bytes > 0)
  449.     {
  450.       if (input_size == 0)
  451.     tape_fill_input_buffer (in_des, io_block_size);
  452.       size = (input_size < num_bytes) ? input_size : num_bytes;
  453.       if (crc_i_flag)
  454.     {
  455.       for (k = 0; k < size; ++k)
  456.         crc += in_buff[k] & 0xff;
  457.     }
  458.       disk_buffered_write (in_buff, out_des, size);
  459.       num_bytes -= size;
  460.       input_size -= size;
  461.       in_buff += size;
  462.     }
  463. }
  464. /* Copy a file using the input and output buffers, which may start out
  465.    partly full.  After the copy, the files are not closed nor the last
  466.    block flushed to output, and the input buffer may still be partly
  467.    full.  If `crc_i_flag' is set, add each byte to `crc'.
  468.    IN_DES is the file descriptor for input;
  469.    OUT_DES is the file descriptor for output;
  470.    NUM_BYTES is the number of bytes to copy.  */
  471.  
  472. void
  473. copy_files_disk_to_tape (in_des, out_des, num_bytes, filename)
  474.      int in_des;
  475.      int out_des;
  476.      long num_bytes;
  477.      char *filename;
  478. {
  479.   long size;
  480.   long k;
  481.   int rc;
  482.   long original_num_bytes;
  483.  
  484.   original_num_bytes = num_bytes;
  485.  
  486.   while (num_bytes > 0)
  487.     {
  488.       if (input_size == 0)
  489.     if (rc = disk_fill_input_buffer (in_des, DISK_IO_BLOCK_SIZE))
  490.       {
  491.         if (rc > 0)
  492.           error (0, 0, "File %s shrunk by %ld bytes, padding with zeros",
  493.                 filename, num_bytes);
  494.         else
  495.           error (0, 0, "Read error at byte %ld in file %s, padding with zeros",
  496.             original_num_bytes - num_bytes, filename);
  497.         write_nuls_to_file (num_bytes, out_des);
  498.         break;
  499.       }
  500.       size = (input_size < num_bytes) ? input_size : num_bytes;
  501.       if (crc_i_flag)
  502.     {
  503.       for (k = 0; k < size; ++k)
  504.         crc += in_buff[k] & 0xff;
  505.     }
  506.       tape_buffered_write (in_buff, out_des, size);
  507.       num_bytes -= size;
  508.       input_size -= size;
  509.       in_buff += size;
  510.     }
  511. }
  512. /* Copy a file using the input and output buffers, which may start out
  513.    partly full.  After the copy, the files are not closed nor the last
  514.    block flushed to output, and the input buffer may still be partly
  515.    full.  If `crc_i_flag' is set, add each byte to `crc'.
  516.    IN_DES is the file descriptor for input;
  517.    OUT_DES is the file descriptor for output;
  518.    NUM_BYTES is the number of bytes to copy.  */
  519.  
  520. void
  521. copy_files_disk_to_disk (in_des, out_des, num_bytes, filename)
  522.      int in_des;
  523.      int out_des;
  524.      long num_bytes;
  525.      char *filename;
  526. {
  527.   long size;
  528.   long k;
  529.   long original_num_bytes;
  530.   int rc;
  531.  
  532.   original_num_bytes = num_bytes;
  533.   while (num_bytes > 0)
  534.     {
  535.       if (input_size == 0)
  536.     if (rc = disk_fill_input_buffer (in_des, DISK_IO_BLOCK_SIZE))
  537.       {
  538.         if (rc > 0)
  539.           error (0, 0, "File %s shrunk by %ld bytes, padding with zeros",
  540.                 filename, num_bytes);
  541.         else
  542.           error (0, 0, "Read error at byte %ld in file %s, padding with zeros",
  543.             original_num_bytes - num_bytes, filename);
  544.         write_nuls_to_file (num_bytes, out_des);
  545.         break;
  546.       }
  547.       size = (input_size < num_bytes) ? input_size : num_bytes;
  548.       if (crc_i_flag)
  549.     {
  550.       for (k = 0; k < size; ++k)
  551.         crc += in_buff[k] & 0xff;
  552.     }
  553.       disk_buffered_write (in_buff, out_des, size);
  554.       num_bytes -= size;
  555.       input_size -= size;
  556.       in_buff += size;
  557.     }
  558. }
  559.  
  560. /* Create all directories up to but not including the last part of NAME.
  561.    Do not destroy any nondirectories while creating directories.  */
  562.  
  563. void
  564. create_all_directories (name)
  565.      char *name;
  566. {
  567.   char *dir;
  568.   int   mode;
  569. #ifdef HPUX_CDF
  570.   int   cdf;
  571. #endif
  572.  
  573.   dir = dirname (name);
  574.   mode = 0700;
  575. #ifdef HPUX_CDF
  576.   cdf = islastparentcdf (name);
  577.   if (cdf)
  578.     {
  579.       dir [strlen (dir) - 1] = '\0';    /* remove final + */
  580.       mode = 04700;
  581.     }
  582.   
  583. #endif
  584.   
  585.   if (dir == NULL)
  586.     error (2, 0, "virtual memory exhausted");
  587.  
  588.   if (dir[0] != '.' || dir[1] != '\0')
  589.     make_path (dir, mode, 0700, -1, -1, (char *) NULL);
  590.  
  591.   free (dir);
  592. }
  593.  
  594. /* Prepare to append to an archive.  We have been in
  595.    process_copy_in, keeping track of the position where
  596.    the last header started in `last_header_start'.  Now we
  597.    have the starting position of the last header (the TRAILER!!!
  598.    header, or blank record for tar archives) and we want to start
  599.    writing (appending) over the last header.  The last header may
  600.    be in the middle of a block, so to keep the buffering in sync
  601.    we lseek back to the start of the block, read everything up
  602.    to but not including the last header, lseek back to the start
  603.    of the block, and then do a copy_buf_out of what we read.
  604.    Actually, we probably don't have to worry so much about keeping the
  605.    buffering perfect since you can only append to archives that
  606.    are disk files.  */
  607.  
  608. void
  609. prepare_append (out_file_des)
  610.      int out_file_des;
  611. {
  612.   int start_of_header;
  613.   int start_of_block;
  614.   int useful_bytes_in_block;
  615.   char *tmp_buf;
  616.  
  617.   start_of_header = last_header_start;
  618.   /* Figure out how many bytes we will rewrite, and where they start.  */
  619.   useful_bytes_in_block = start_of_header % io_block_size;
  620.   start_of_block = start_of_header - useful_bytes_in_block;
  621.  
  622.   if (lseek (out_file_des, start_of_block, SEEK_SET) < 0)
  623.     error (1, errno, "cannot seek on output");
  624.   if (useful_bytes_in_block > 0)
  625.     {
  626.       tmp_buf = (char *) xmalloc (useful_bytes_in_block);
  627.       read (out_file_des, tmp_buf, useful_bytes_in_block);
  628.       if (lseek (out_file_des, start_of_block, SEEK_SET) < 0)
  629.     error (1, errno, "cannot seek on output");
  630.       /* fix juo -- is this copy_tape_buf_out?  or copy_disk? */
  631.       tape_buffered_write (tmp_buf, out_file_des, useful_bytes_in_block);
  632.       free (tmp_buf);
  633.     }
  634.  
  635.   /* We are done reading the archive, so clear these since they
  636.      will now be used for reading in files that we are appending
  637.      to the archive.  */
  638.   input_size = 0;
  639.   input_bytes = 0;
  640.   in_buff = input_buffer;
  641. }
  642.  
  643. /* Support for remembering inodes with multiple links.  Used in the
  644.    "copy in" and "copy pass" modes for making links instead of copying
  645.    the file.  */
  646.  
  647. struct inode_val
  648. {
  649.   unsigned long inode;
  650.   unsigned long major_num;
  651.   unsigned long minor_num;
  652.   char *file_name;
  653. };
  654.  
  655. /* Inode hash table.  Allocated by first call to add_inode.  */
  656. static struct inode_val **hash_table = NULL;
  657.  
  658. /* Size of current hash table.  Initial size is 47.  (47 = 2*22 + 3) */
  659. static int hash_size = 22;
  660.  
  661. /* Number of elements in current hash table.  */
  662. static int hash_num;
  663.  
  664. /* Find the file name associated with NODE_NUM.  If there is no file
  665.    associated with NODE_NUM, return NULL.  */
  666.  
  667. char *
  668. find_inode_file (node_num, major_num, minor_num)
  669.      unsigned long node_num;
  670.      unsigned long major_num;
  671.      unsigned long minor_num;
  672. {
  673. #ifndef __MSDOS__
  674.   int start;            /* Initial hash location.  */
  675.   int temp;            /* Rehash search variable.  */
  676.  
  677.   if (hash_table != NULL)
  678.     {
  679.       /* Hash function is node number modulo the table size.  */
  680.       start = node_num % hash_size;
  681.  
  682.       /* Initial look into the table.  */
  683.       if (hash_table[start] == NULL)
  684.     return NULL;
  685.       if (hash_table[start]->inode == node_num
  686.       && hash_table[start]->major_num == major_num
  687.       && hash_table[start]->minor_num == minor_num)
  688.     return hash_table[start]->file_name;
  689.  
  690.       /* The home position is full with a different inode record.
  691.      Do a linear search terminated by a NULL pointer.  */
  692.       for (temp = (start + 1) % hash_size;
  693.        hash_table[temp] != NULL && temp != start;
  694.        temp = (temp + 1) % hash_size)
  695.     {
  696.       if (hash_table[temp]->inode == node_num
  697.           && hash_table[start]->major_num == major_num
  698.           && hash_table[start]->minor_num == minor_num)
  699.         return hash_table[temp]->file_name;
  700.     }
  701.     }
  702. #endif
  703.   return NULL;
  704. }
  705.  
  706. /* Associate FILE_NAME with the inode NODE_NUM.  (Insert into hash table.)  */
  707.  
  708. void
  709. add_inode (node_num, file_name, major_num, minor_num)
  710.      unsigned long node_num;
  711.      char *file_name;
  712.      unsigned long major_num;
  713.      unsigned long minor_num;
  714. {
  715. #ifndef __MSDOS__
  716.   struct inode_val *temp;
  717.  
  718.   /* Create new inode record.  */
  719.   temp = (struct inode_val *) xmalloc (sizeof (struct inode_val));
  720.   temp->inode = node_num;
  721.   temp->major_num = major_num;
  722.   temp->minor_num = minor_num;
  723.   temp->file_name = xstrdup (file_name);
  724.  
  725.   /* Do we have to increase the size of (or initially allocate)
  726.      the hash table?  */
  727.   if (hash_num == hash_size || hash_table == NULL)
  728.     {
  729.       struct inode_val **old_table;    /* Pointer to old table.  */
  730.       int i;            /* Index for re-insert loop.  */
  731.  
  732.       /* Save old table.  */
  733.       old_table = hash_table;
  734.       if (old_table == NULL)
  735.     hash_num = 0;
  736.  
  737.       /* Calculate new size of table and allocate it.
  738.          Sequence of table sizes is 47, 97, 197, 397, 797, 1597, 3197, 6397 ...
  739.      where 3197 and most of the sizes after 6397 are not prime.  The other
  740.      numbers listed are prime.  */
  741.       hash_size = 2 * hash_size + 3;
  742.       hash_table = (struct inode_val **)
  743.     xmalloc (hash_size * sizeof (struct inode_val *));
  744.       bzero (hash_table, hash_size * sizeof (struct inode_val *));
  745.  
  746.       /* Insert the values from the old table into the new table.  */
  747.       for (i = 0; i < hash_num; i++)
  748.     hash_insert (old_table[i]);
  749.  
  750.       if (old_table != NULL)
  751.     free (old_table);
  752.     }
  753.  
  754.   /* Insert the new record and increment the count of elements in the
  755.       hash table.  */
  756.   hash_insert (temp);
  757.   hash_num++;
  758. #endif /* __MSDOS__ */
  759. }
  760.  
  761. /* Do the hash insert.  Used in normal inserts and resizing the hash
  762.    table.  It is guaranteed that there is room to insert the item.
  763.    NEW_VALUE is the pointer to the previously allocated inode, file
  764.    name association record.  */
  765.  
  766. static void
  767. hash_insert (new_value)
  768.      struct inode_val *new_value;
  769. {
  770.   int start;            /* Home position for the value.  */
  771.   int temp;            /* Used for rehashing.  */
  772.  
  773.   /* Hash function is node number modulo the table size.  */
  774.   start = new_value->inode % hash_size;
  775.  
  776.   /* Do the initial look into the table.  */
  777.   if (hash_table[start] == NULL)
  778.     {
  779.       hash_table[start] = new_value;
  780.       return;
  781.     }
  782.  
  783.   /* If we get to here, the home position is full with a different inode
  784.      record.  Do a linear search for the first NULL pointer and insert
  785.      the new item there.  */
  786.   temp = (start + 1) % hash_size;
  787.   while (hash_table[temp] != NULL)
  788.     temp = (temp + 1) % hash_size;
  789.  
  790.   /* Insert at the NULL.  */
  791.   hash_table[temp] = new_value;
  792. }
  793.  
  794. /* Open FILE in the mode specified by the command line options
  795.    and return an open file descriptor for it,
  796.    or -1 if it can't be opened.  */
  797.  
  798. int
  799. open_archive (file)
  800.      char *file;
  801. {
  802.   int fd;
  803.   void (*copy_in) ();        /* Workaround for pcc bug.  */
  804.  
  805.   copy_in = process_copy_in;
  806.  
  807.   if (copy_function == copy_in)
  808.     fd = rmtopen (file, O_RDONLY | O_BINARY, 0666);
  809.   else
  810.     {
  811.       if (!append_flag)
  812.     fd = rmtopen (file, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
  813.       else
  814.     fd = rmtopen (file, O_RDWR | O_BINARY, 0666);
  815.     }
  816.  
  817.   return fd;
  818. }
  819.  
  820. /* Attempt to rewind the tape drive on file descriptor TAPE_DES
  821.    and take it offline.  */
  822.  
  823. void
  824. tape_offline (tape_des)
  825.      int tape_des;
  826. {
  827. #if defined(MTIOCTOP) && defined(MTOFFL)
  828.   struct mtop control;
  829.  
  830.   control.mt_op = MTOFFL;
  831.   control.mt_count = 1;
  832.   rmtioctl (tape_des, MTIOCTOP, &control);    /* Don't care if it fails.  */
  833. #endif
  834. }
  835.  
  836. /* The file on file descriptor TAPE_DES is assumed to be magnetic tape
  837.    (or floppy disk or other device) and the end of the medium
  838.    has been reached.  Ask the user for to mount a new "tape" to continue
  839.    the processing.  If the user specified the device name on the
  840.    command line (with the -I, -O, -F or --file options), then we can
  841.    automatically re-open the same device to use the next medium.  If the
  842.    user did not specify the device name, then we have to ask them which
  843.    device to use.  */
  844.  
  845. void
  846. get_next_reel (tape_des)
  847.      int tape_des;
  848. {
  849.   static int reel_number = 1;
  850.   FILE *tty_in;            /* File for interacting with user.  */
  851.   FILE *tty_out;        /* File for interacting with user.  */
  852.   int old_tape_des;
  853.   char *next_archive_name;
  854.   dynamic_string new_name;
  855.   char *str_res;
  856.  
  857.   ds_init (&new_name, 128);
  858.  
  859.   /* Open files for interactive communication.  */
  860.   tty_in = fopen (CONSOLE, "r");
  861.   if (tty_in == NULL)
  862.     error (2, errno, CONSOLE);
  863.   tty_out = fopen (CONSOLE, "w");
  864.   if (tty_out == NULL)
  865.     error (2, errno, CONSOLE);
  866.  
  867.   old_tape_des = tape_des;
  868.   tape_offline (tape_des);
  869.   rmtclose (tape_des);
  870.  
  871.   /* Give message and wait for carrage return.  User should hit carrage return
  872.      only after loading the next tape.  */
  873.   ++reel_number;
  874.   if (new_media_message)
  875.     fprintf (tty_out, "%s", new_media_message);
  876.   else if (new_media_message_with_number)
  877.     fprintf (tty_out, "%s%d%s", new_media_message_with_number, reel_number,
  878.          new_media_message_after_number);
  879.   else if (archive_name)
  880.     fprintf (tty_out, "Found end of tape.  Load next tape and press RETURN. ");
  881.   else
  882.     fprintf (tty_out, "Found end of tape.  To continue, type device/file name when ready.\n");
  883.  
  884.   fflush (tty_out);
  885.  
  886.   if (archive_name)
  887.     {
  888.       int c;
  889.  
  890.       do
  891.     c = getc (tty_in);
  892.       while (c != EOF && c != '\n');
  893.  
  894.       tape_des = open_archive (archive_name);
  895.       if (tape_des == -1)
  896.     error (1, errno, "%s", archive_name);
  897.     }
  898.   else
  899.     {
  900.       do
  901.     {
  902.       if (tape_des < 0)
  903.         {
  904.           fprintf (tty_out,
  905.                "To continue, type device/file name when ready.\n");
  906.           fflush (tty_out);
  907.         }
  908.  
  909.       str_res = ds_fgets (tty_in, &new_name);
  910.       if (str_res == NULL || str_res[0] == '\0')
  911.         exit (1);
  912.       next_archive_name = str_res;
  913.  
  914.       tape_des = open_archive (next_archive_name);
  915.       if (tape_des == -1)
  916.         error (0, errno, "%s", next_archive_name);
  917.     }
  918.       while (tape_des < 0);
  919.     }
  920.  
  921.   /* We have to make sure that `tape_des' has not changed its value even
  922.      though we closed it and reopened it, since there are local
  923.      copies of it in other routines.  This works fine on Unix (even with
  924.      rmtread and rmtwrite) since open will always return the lowest
  925.      available file descriptor and we haven't closed any files (e.g.,
  926.      stdin, stdout or stderr) that were opened before we originally opened
  927.      the archive.  */
  928.  
  929.   if (tape_des != old_tape_des)
  930.     error (1, 0, "internal error: tape descriptor changed from %d to %d",
  931.        old_tape_des, tape_des);
  932.  
  933.   free (new_name.ds_string);
  934.   fclose (tty_in);
  935.   fclose (tty_out);
  936. }
  937.  
  938. /* If MESSAGE does not contain the string "%d", make `new_media_message'
  939.    a copy of MESSAGE.  If MESSAGES does contain the string "%d", make
  940.    `new_media_message_with_number' a copy of MESSAGE up to, but
  941.    not including, the string "%d", and make `new_media_message_after_number'
  942.    a copy of MESSAGE after the string "%d".  */
  943.  
  944. void
  945. set_new_media_message (message)
  946.      char *message;
  947. {
  948.   char *p;
  949.   int prev_was_percent;
  950.  
  951.   p = message;
  952.   prev_was_percent = 0;
  953.   while (*p != '\0')
  954.     {
  955.       if (*p == 'd' && prev_was_percent)
  956.     break;
  957.       prev_was_percent = (*p == '%');
  958.       ++p;
  959.     }
  960.   if (*p == '\0')
  961.     {
  962.       new_media_message = xstrdup (message);
  963.     }
  964.   else
  965.     {
  966.       int length = p - message - 1;
  967.  
  968.       new_media_message_with_number = xmalloc (length + 1);
  969.       strncpy (new_media_message_with_number, message, length);
  970.       new_media_message_with_number[length] = '\0';
  971.       length = strlen (p + 1);
  972.       new_media_message_after_number = xmalloc (length + 1);
  973.       strcpy (new_media_message_after_number, p + 1);
  974.     }
  975. }
  976.  
  977. #ifdef SYMLINK_USES_UMASK
  978. /* Most machines always create symlinks with rwxrwxrwx protection,
  979.    but some (HP/UX 8.07; maybe DEC's OSF on MIPS, too?) use the
  980.    umask when creating symlinks, so if your umask is 022 you end
  981.    up with rwxr-xr-x symlinks (although HP/UX seems to completely
  982.    ignore the protection).  There doesn't seem to be any way to
  983.    manipulate the modes once the symlinks are created (e.g.
  984.    a hypothetical "lchmod"), so to create them with the right
  985.    modes we have to set the umask first.  */
  986.  
  987. int
  988. umasked_symlink (name1, name2, mode)
  989.   char *name1;
  990.   char *name2;
  991.   int mode;
  992. {
  993.   int    old_umask;
  994.   int    rc;
  995.   mode = ~(mode & 0777) & 0777;
  996.   old_umask = umask (mode);
  997.   rc = symlink (name1, name2);
  998.   umask (old_umask);
  999.   return rc;
  1000. }
  1001. #endif /* SYMLINK_USES_UMASK */
  1002.  
  1003. #if defined(__MSDOS__) && !defined(__GNUC__) || defined(__EMX__)
  1004. int
  1005. chown (path, owner, group)
  1006.      char *path;
  1007.      int owner, group;
  1008. {
  1009.   return 0;
  1010. }
  1011. #endif
  1012.  
  1013. #ifdef __TURBOC__
  1014. #include <time.h>
  1015. #include <fcntl.h>
  1016. #include <io.h>
  1017.  
  1018. int
  1019. utime (char *filename, struct utimbuf *utb)
  1020. {
  1021.   extern int errno;
  1022.   struct tm *tm;
  1023.   struct ftime filetime;
  1024.   time_t when;
  1025.   int fd;
  1026.   int status;
  1027.  
  1028.   if (utb == 0)
  1029.       when = time (0);
  1030.   else
  1031.       when = utb->modtime;
  1032.  
  1033.     fd = _open (filename, O_RDWR);
  1034.   if (fd == -1)
  1035.       return -1;
  1036.  
  1037.     tm = localtime (&when);
  1038.   if (tm->tm_year < 80)
  1039.       filetime.ft_year = 0;
  1040.   else
  1041.       filetime.ft_year = tm->tm_year - 80;
  1042.     filetime.ft_month = tm->tm_mon + 1;
  1043.     filetime.ft_day = tm->tm_mday;
  1044.   if (tm->tm_hour < 0)
  1045.       filetime.ft_hour = 0;
  1046.   else
  1047.       filetime.ft_hour = tm->tm_hour;
  1048.     filetime.ft_min = tm->tm_min;
  1049.     filetime.ft_tsec = tm->tm_sec / 2;
  1050.  
  1051.     status = setftime (fd, &filetime);
  1052.     _close (fd);
  1053.     return status;
  1054. }
  1055. #endif
  1056. #ifdef HPUX_CDF
  1057. /* When we create a cpio archive we mark CDF's by putting an extra `/'
  1058.    after their component name so we can distinguish the CDF's when we
  1059.    extract the archive (in case the "hidden" directory's files appear
  1060.    in the archive before the directory itself).  E.g., in the path
  1061.    "a/b+/c", if b+ is a CDF, we will write this path as "a/b+//c" in
  1062.    the archive so when we extract the archive we will know that b+
  1063.    is actually a CDF, and not an ordinary directory whose name happens
  1064.    to end in `+'.  We also do the same thing internally in copypass.c.  */
  1065.  
  1066.  
  1067. /* Take an input pathname and check it for CDF's.  Insert an extra
  1068.    `/' in the pathname after each "hidden" directory.  If we add
  1069.    any `/'s, return a malloced string (which it will reuse for
  1070.    later calls so our caller doesn't have to worry about freeing
  1071.    the string) instead of the original input string.  */
  1072.  
  1073. char *
  1074. add_cdf_double_slashes (input_name)
  1075.   char *input_name;
  1076. {
  1077.   static char *ret_name = NULL;    /* re-usuable return buffer (malloc'ed)  */
  1078.   static int ret_size = -1;    /* size of return buffer.  */
  1079.   char *p;
  1080.   char *q;
  1081.   int n;
  1082.   struct stat dir_stat;
  1083.  
  1084.   /*  Search for a `/' preceeded by a `+'.  */
  1085.  
  1086.   for (p = input_name; *p != '\0'; ++p)
  1087.     {
  1088.       if ( (*p == '+') && (*(p + 1) == '/') )
  1089.     break;
  1090.     }
  1091.  
  1092.   /* If we didn't find a `/' preceeded by a `+' then there are
  1093.      no CDF's in this pathname.  Return the original pathname.  */
  1094.  
  1095.   if (*p == '\0')
  1096.     return input_name;
  1097.  
  1098.   /* There was a `/' preceeded by a `+' in the pathname.  If it is a CDF 
  1099.      then we will need to copy the input pathname to our return
  1100.      buffer so we can insert the extra `/'s.  Since we can't tell
  1101.      yet whether or not it is a CDF we will just always copy the
  1102.      string to the return buffer.  First we have to make sure the
  1103.      buffer is large enough to hold the string and any number of
  1104.      extra `/'s we might add.  */
  1105.  
  1106.   n = 2 * (strlen (input_name) + 1);
  1107.   if (n >= ret_size)
  1108.     {
  1109.       if (ret_size < 0)
  1110.     ret_name = (char *) malloc (n);
  1111.       else
  1112.     ret_name = (char *)realloc (ret_name, n);
  1113.       ret_size = n;
  1114.     }
  1115.  
  1116.   /* Clear the `/' after this component, so we can stat the pathname 
  1117.      up to and including this component.  */
  1118.   ++p;
  1119.   *p = '\0';
  1120.   if ((*xstat) (input_name, &dir_stat) < 0)
  1121.     {
  1122.       error (0, errno, "%s", input_name);
  1123.       return input_name;
  1124.     }
  1125.  
  1126.   /* Now put back the `/' after this component and copy the pathname up to
  1127.      and including this component and its trailing `/' to the return
  1128.      buffer.  */
  1129.   *p++ = '/';
  1130.   strncpy (ret_name, input_name, p - input_name);
  1131.   q = ret_name + (p - input_name);
  1132.  
  1133.   /* If it was a CDF, add another `/'.  */
  1134.   if (S_ISDIR (dir_stat.st_mode) && (dir_stat.st_mode & 04000) )
  1135.     *q++ = '/';
  1136.  
  1137.   /* Go through the rest of the input pathname, copying it to the
  1138.      return buffer, and adding an extra `/' after each CDF.  */
  1139.   while (*p != '\0')
  1140.     {
  1141.       if ( (*p == '+') && (*(p + 1) == '/') )
  1142.     {
  1143.       *q++ = *p++;
  1144.  
  1145.       *p = '\0';
  1146.       if ((*xstat) (input_name, &dir_stat) < 0)
  1147.         {
  1148.           error (0, errno, "%s", input_name);
  1149.           return input_name;
  1150.         }
  1151.       *p = '/';
  1152.  
  1153.       if (S_ISDIR (dir_stat.st_mode) && (dir_stat.st_mode & 04000) )
  1154.         *q++ = '/';
  1155.     }
  1156.       *q++ = *p++;
  1157.     }
  1158.   *q = '\0';
  1159.  
  1160.   return ret_name;
  1161. }
  1162.  
  1163. /* Is the last parent directory (e.g., c in a/b/c/d) a CDF?  If the
  1164.    directory name ends in `+' and is followed by 2 `/'s instead of 1
  1165.    then it is.  This is only the case for cpio archives, but we don't
  1166.    have to worry about tar because tar always has the directory before
  1167.    its files (or else we lose).  */
  1168.  
  1169. islastparentcdf(path)
  1170.   char    *path;
  1171. {
  1172.   char *newpath;
  1173.   char *slash;
  1174.   int slash_count;
  1175.   int length;            /* Length of result, not including NUL.  */
  1176.  
  1177.   slash = rindex (path, '/');
  1178.   if (slash == 0)
  1179.     return 0;
  1180.   else
  1181.     {
  1182.       slash_count = 0;
  1183.       while (slash > path && *slash == '/')
  1184.     {
  1185.       ++slash_count;
  1186.       --slash;
  1187.     }
  1188.  
  1189.  
  1190.       if ( (*slash == '+') && (slash_count >= 2) )
  1191.     return 1;
  1192.     }
  1193.   return 0;
  1194. }
  1195. #endif
  1196.  
  1197. #define DISKBLOCKSIZE    (512)
  1198.  
  1199. enum sparse_write_states { begin, in_zeros, not_in_zeros };
  1200.  
  1201.  
  1202. static int
  1203. buf_all_zeros (buf, bufsize)
  1204.   char *buf;
  1205.   int bufsize;
  1206. {
  1207.   int    i;
  1208.   for (i = 0; i < bufsize; ++i)
  1209.     {
  1210.       if (*buf++ != '\0')
  1211.     return 0;
  1212.     }
  1213.   return 1;
  1214. }
  1215.  
  1216. int delayed_seek_count = 0;
  1217.  
  1218. /* Write NBYTE bytes from BUF to remote tape connection FILDES.
  1219.    Return the number of bytes written on success, -1 on error.  */
  1220.  
  1221. int
  1222. sparse_write (fildes, buf, nbyte)
  1223.      int fildes;
  1224.      char *buf;
  1225.      unsigned int nbyte;
  1226. {
  1227.   int complete_block_count;
  1228.   int leftover_bytes_count;
  1229.   int seek_count;
  1230.   int write_count;
  1231.   char *cur_write_start;
  1232.   int lseek_rc;
  1233.   int write_rc;
  1234.   int i;
  1235.   enum sparse_write_states state;
  1236.  
  1237.   complete_block_count = nbyte / DISKBLOCKSIZE;
  1238.   leftover_bytes_count = nbyte % DISKBLOCKSIZE;
  1239.  
  1240.   if (delayed_seek_count != 0)
  1241.     state = in_zeros;
  1242.   else
  1243.     state = begin;
  1244.  
  1245.   seek_count = delayed_seek_count;
  1246.  
  1247.   for (i = 0; i < complete_block_count; ++i)
  1248.     {
  1249.       switch (state)
  1250.     {
  1251.       case begin :
  1252.         if (buf_all_zeros (buf, DISKBLOCKSIZE))
  1253.           {
  1254.         seek_count = DISKBLOCKSIZE;
  1255.         state = in_zeros;
  1256.           }
  1257.         else
  1258.           {
  1259.         cur_write_start = buf;
  1260.         write_count = DISKBLOCKSIZE;
  1261.         state = not_in_zeros;
  1262.           }
  1263.         buf += DISKBLOCKSIZE;
  1264.         break;
  1265.       case in_zeros :
  1266.         if (buf_all_zeros (buf, DISKBLOCKSIZE))
  1267.           {
  1268.         seek_count += DISKBLOCKSIZE;
  1269.           }
  1270.         else
  1271.           {
  1272.         lseek (fildes, seek_count, SEEK_CUR);
  1273.         cur_write_start = buf;
  1274.         write_count = DISKBLOCKSIZE;
  1275.         state = not_in_zeros;
  1276.           }
  1277.         buf += DISKBLOCKSIZE;
  1278.         break;
  1279.       case not_in_zeros :
  1280.         if (buf_all_zeros (buf, DISKBLOCKSIZE))
  1281.           {
  1282.         write_rc = write (fildes, cur_write_start, write_count);
  1283.         seek_count = DISKBLOCKSIZE;
  1284.         state = in_zeros;
  1285.           }
  1286.         else
  1287.           {
  1288.         write_count += DISKBLOCKSIZE;
  1289.           }
  1290.         buf += DISKBLOCKSIZE;
  1291.         break;
  1292.     }
  1293.     }
  1294.  
  1295.   switch (state)
  1296.     {
  1297.       case begin :
  1298.       case in_zeros :
  1299.     delayed_seek_count = seek_count;
  1300.     break;
  1301.       case not_in_zeros :
  1302.     write_rc = write (fildes, cur_write_start, write_count);
  1303.     delayed_seek_count = 0;
  1304.     break;
  1305.     }
  1306.  
  1307.   if (leftover_bytes_count != 0)
  1308.     {
  1309.       if (delayed_seek_count != 0)
  1310.     {
  1311.       lseek_rc = lseek (fildes, delayed_seek_count, SEEK_CUR);
  1312.       delayed_seek_count = 0;
  1313.     }
  1314.       write_rc = write (fildes, buf, leftover_bytes_count);
  1315.     }
  1316.   return nbyte;
  1317. }
  1318.  
  1319. static void
  1320. write_nuls_to_file (num_bytes, out_des)
  1321.     long    num_bytes;
  1322.     int        out_des;
  1323. {
  1324.   long    blocks;
  1325.   long    extra_bytes;
  1326.   long    i;
  1327.  
  1328.   blocks = num_bytes / 512;
  1329.   extra_bytes = num_bytes % 512;
  1330.   for (i = 0; i < extra_bytes; ++i)
  1331.     {
  1332.       if (write (out_des, zeros_512, 512) != 512)
  1333.     error (1, errno, "error writing NUL's");
  1334.     }
  1335.   if (extra_bytes != 0)
  1336.     {
  1337.       if (write (out_des, zeros_512, extra_bytes) != extra_bytes)
  1338.     error (1, errno, "error writing NUL's");
  1339.     }
  1340. }
  1341.